<?php

namespace app\common;

use app\model\User;
use app\model\UserCollect;
use app\model\UserFollow;
use app\model\UserGoodsDevoteLog;
use app\model\UserGoodsQuotaLog;
use app\model\UserLayer;
use app\model\UserLike;
use app\model\UserMoneyLog;
use app\model\UserScoreLog;
use app\model\UserShopQuotaLog;
use app\model\UserSuperQuotaLog;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\facade\Db;

class UserService
{
	
	/**
	 * 收藏
	 * @param $id
	 * @param $uid
	 * @param $type
	 * @return bool
	 */
	public static function collect($id, $uid, $type)
	{
		
		$row = UserCollect::where(['user_id' => $uid, 'pro_id' => $id, 'type' => $type])->find();
		if ($row) {
			$row->delete();
			return false;
			
		} else {
			UserCollect::create([
				'user_id' => $uid,
				'pro_id' => $id,
				'type' => $type,
			]);
			return true;
		}
	}
	
	#点赞
	public static function like($id, $uid, $type)
	{
		$row = UserLike::where(['user_id' => $uid, 'pro_id' => $id, 'type' => $type])->find();
		if ($row) {
			$row->delete();
			return false;
			
		} else {
			UserLike::create([
				'user_id' => $uid,
				'pro_id' => $id,
				'type' => $type
			]);
			return true;
		}
	}
	
	public static function follow($id, $uid, $type)
	{
		$row = UserFollow::where(['user_id' => $uid, 'pro_id' => $id, 'type' => $type])->find();
		if ($row) {
			$row->delete();
			return false;
			
		} else {
			UserFollow::create([
				'user_id' => $uid,
				'pro_id' => $id,
				'type' => $type
			]);
			return true;
		}
	}
	
	/**
	 * 变更积分
	 * @param $score
	 * @param $uid
	 * @param $memo
	 * @return true
	 * @throws Exception
	 */
	public static function changeUserScore($score, $uid, $memo)
	{
		// 启动事务
		Db::startTrans();
		try {
            $row = User::lock(true)->find($uid);
            if (!$row) {
                throw new Exception('变更积分失败');
            }
            if ($score < 0 && $row->score < abs($score)) {
                throw new Exception('积分不足');
            }
            $before = $row->score;
            $row->score += $score;
            $data = [
                'user_id' => $uid,
                'score' => $score,
                'before' => $before,
                'after' => $row->score,
                'memo' => $memo
            ];
			UserScoreLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Throwable $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception($e->getMessage());
		}
		return true;
	}
	
	/**
	 * 更改用户余额
	 * @param $money
	 * @param $uid
	 * @param $memo
	 * @return bool
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws Exception
	 * @throws ModelNotFoundException
	 */
	public static function changeUserMoney($money, $uid, $memo): bool
	{
		
		$row = User::lock(true)->find($uid);
		if ($money == 0 || !$row) {
			throw new Exception('变更金额失败');
		}
		
		if ($money < 0 && $row->money < abs($money)) {
			throw new Exception('余额不足');
		}
		
		$before = $row->money;
		$row->money += $money;
		$data = [
			'user_id' => $uid,
			'money' => $money,
			'before' => $before,
			'after' => $row->money,
			'memo' => $memo
		];
		
		// 启动事务
		Db::startTrans();
		try {
			UserMoneyLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception('余额变更失败');
		}
		return true;
	}
	
	
	/**
	 * 更改精品区贡献值
	 * @param $goods_devote
	 * @param $uid
	 * @param $memo
	 * @return bool
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws Exception
	 * @throws ModelNotFoundException
	 */
	public static function changeUserGoodsDevote($goods_devote, $uid, $memo): bool
	{
		
		$row = User::find($uid);
		if ($goods_devote == 0 || !$row) {
			throw new Exception('变更精品区贡献值失败');
		}
		
		if ($goods_devote < 0 && $row->goods_devote < abs($goods_devote)) {
			throw new Exception('精品区贡献值不足');
		}
		
		$before = $row->goods_devote;
		$row->goods_devote += $goods_devote;
		if ($memo == '购买精品区商品'){
			#给上面的每个人都增加团队贡献值 并且升级
			$layers = UserLayer::where(['user_id'=>$row->id])->select();
			foreach ($layers as $layer){
				if ($layer->parent){
					$layer->parent->goods_team_devote += $goods_devote;
					if ($layer->parent->level == 1 && $layer->parent->goods_team_devote >= 50){
						#判断团队贡献值有没有达到50
						$layer->parent->level = 2;
					}
					if ($layer->parent->level == 2 && $layer->parent->goods_team_devote >= 500){
						#判断团队贡献值有没有达到500
						$layer->parent->level = 3;
					}
					if ($layer->parent->level == 3 && $layer->parent->goods_team_devote >= 5000){
						#判断团队贡献值有没有达到5000
						$layer->parent->level = 4;
					}
					if ($layer->parent->level == 4 && $layer->parent->goods_team_devote >= 15000){
						#判断团队贡献值有没有达到15000
						$layer->parent->level = 5;
					}
					if ($layer->parent->level == 5 && $layer->parent->goods_team_devote >= 30000){
						#判断团队贡献值有没有达到30000
						$layer->parent->level = 6;
					}
					$layer->together(['parent'])->save();
				}
			}
		}
		$data = [
			'user_id' => $uid,
			'goods_devote' => $goods_devote,
			'before' => $before,
			'after' => $row->goods_devote,
			'memo' => $memo
		];
		
		// 启动事务
		Db::startTrans();
		try {
			UserGoodsDevoteLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception('精品区贡献值变更失败');
		}
		return true;
	}
	
	/**
	 * 更改精品区分红额度
	 * @param $goods_quota
	 * @param $uid
	 * @param $memo
	 * @return bool
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws Exception
	 * @throws ModelNotFoundException
	 */
	public static function changeUserGoodsQuota($goods_quota, $uid, $memo): bool
	{
		
		$row = User::find($uid);
		if ($goods_quota == 0 || !$row) {
			throw new Exception('变更精品区分红额度失败');
		}
		
		if ($goods_quota < 0 && $row->goods_quota < abs($goods_quota)) {
			throw new Exception('精品区分红额度不足');
		}
		
		$before = $row->goods_quota;
		$row->goods_quota += $goods_quota;
		$data = [
			'user_id' => $uid,
			'goods_quota' => $goods_quota,
			'before' => $before,
			'after' => $row->goods_quota,
			'memo' => $memo
		];
		
		// 启动事务
		Db::startTrans();
		try {
			UserGoodsQuotaLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception('精品区分红额度变更失败');
		}
		return true;
	}
	
	/**
	 * 更改商家分红额度
	 * @param $shop_quota
	 * @param $uid
	 * @param $memo
	 * @return bool
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws Exception
	 * @throws ModelNotFoundException
	 */
	public static function changeUserShopQuota($shop_quota, $uid, $memo): bool
	{
		
		$row = User::find($uid);
		if ($shop_quota == 0 || !$row) {
			throw new Exception('变更商家分红额度失败');
		}
		
		if ($shop_quota < 0 && $row->shop_quota < abs($shop_quota)) {
			throw new Exception('商家分红额度不足');
		}
		
		$before = $row->shop_quota;
		$row->shop_quota += $shop_quota;
		$data = [
			'user_id' => $uid,
			'shop_quota' => $shop_quota,
			'before' => $before,
			'after' => $row->shop_quota,
			'memo' => $memo
		];
		
		// 启动事务
		Db::startTrans();
		try {
			UserShopQuotaLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception('商家分红额度变更失败');
		}
		return true;
	}
	
	/**
	 * 更改优选区分红额度
	 * @param $super_quota
	 * @param $uid
	 * @param $memo
	 * @return bool
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws Exception
	 * @throws ModelNotFoundException
	 */
	public static function changeUserSuperQuota($super_quota, $uid, $memo): bool
	{
		
		$row = User::find($uid);
		if ($super_quota == 0 || !$row) {
			throw new Exception('变更优选区分红额度失败');
		}
		
		if ($super_quota < 0 && $row->super_quota < abs($super_quota)) {
			throw new Exception('优选区分红额度不足');
		}
		
		$before = $row->super_quota;
		$row->super_quota += $super_quota;
		$data = [
			'user_id' => $uid,
			'super_quota' => $super_quota,
			'before' => $before,
			'after' => $row->super_quota,
			'memo' => $memo
		];
		
		// 启动事务
		Db::startTrans();
		try {
			UserSuperQuotaLog::create($data);
			$row->save();
			// 提交事务
			Db::commit();
		} catch (\Exception $e) {
			// 回滚事务
			Db::rollback();
			throw new Exception('优选区分红额度变更失败');
		}
		return true;
	}
	
	/**
	 * 记录足迹
	 * @param $id
	 * @param $uid
	 * @return bool
	 */
	public static function changeUserFoot($id, $uid)
	{
		$row = UserFoot::where(['user_id' => $uid, 'goods_id' => $id])->find();
		if ($row) {
			$row->updatetime = time();
			$row->save();
			return false;
		} else {
			UserFoot::create([
				'user_id' => $uid,
				'goods_id' => $id,
			]);
			return true;
		}
	}
	
	
}